package com.strom.concurrent;

import com.google.common.util.concurrent.*;

import java.util.concurrent.Executors;

public class Future {

    public static void main(String[] args) throws Exception {
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
        ListenableFuture<String> future1 = service.submit(() -> {
            Thread.currentThread().sleep(3000);//模拟延迟
            return "first";
        });
      //  future1.addListener(() -> System.out.println("111"), service);

        Futures.addCallback(future1, new FutureCallback<String>() {
            @Override
            public void onSuccess(String s) {
                System.out.println(s);
            }

            @Override
            public void onFailure(Throwable throwable) {
                System.out.println(throwable.getCause());
            }
        }, service);

        Futures.getDone(future1);
    }


}
